import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import * as EleIcon from '@element-plus/icons-vue'

import axios from 'axios'
import VueAxios from 'vue-axios'

import ECharts from 'vue-echarts'
import './utils/echarts'

import './utils/mock'
import "./assets/styles/index.less"

import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'

const app = createApp(App);
for (let name in EleIcon) {
  app.component(name, (EleIcon as any)[name])
}

// app.config.globalProperties.$axios = axios
// register globally (or you can do it locally)
app.component('v-chart', ECharts)

router.beforeEach((to, from ,next) => {
  store.commit('GETTOKEN');
  const token = store.state.user.token;
  if (!token && to.name !== 'Login') {
    next({ name: 'Login' });
  } else if (token && to.matched.length === 0) {
    // 刷新后没有动态路由了
    store.commit('ADDROUTER', router);
    next( { path: to.path } );
  } else if (token && to.name === 'Login') {
    next( { name: 'Index' } );
  } else {
    next();
  }
})

app.use(router)
  .use(store)
  .use(VueAxios, axios)
  .use(ElementPlus, {
    locale: zhCn,
  })
app.mount('#app')

